package structure.map;

import java.util.HashMap;
import java.util.TreeMap;

import org.junit.Test;

import definition.comparable.SubClass;
import definition.comparable.SuperClass;

/**
 * 二叉树映射演示
 * @author fidel
 */
public class TreeMapDemo {
    @Test
    public void test() {
        /*
         * TreeMap类实现的SortedMap接口根据其键的自然顺序（Comparable）或在创建该接口实例时提供的Comparator进行排序
         * 如果两个对象相等并具有相同的哈希值且属于同一类，即使它们的自然排序不一致，在SortedMap接口中也被视为不同的键
         */
        TreeMap<? extends SuperClass, String> map = new TreeMap<>();
        SubClass a = new SubClass();
        SubClass b = new SubClass();
        @SuppressWarnings("unchecked")
        TreeMap<SuperClass, String> treeMap = (TreeMap<SuperClass, String>) map;
        treeMap.put(a, "value");
        treeMap.put(b, "one");
        System.out.println(treeMap.size());
        // HashMap类以键的hashCode和equals方法返回值判断是否是同一个键
        HashMap<SubClass, String> hashMap = new HashMap<>(16);
        hashMap.put(new SubClass(), "key");
        hashMap.put(new SubClass(), "map");
        System.out.println(hashMap.size());
    }
}
